package com.see51.demo.see51demo.Frag;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.see51.demo.see51demo.CPlayerActivity;
import com.see51.demo.see51demo.DevInfo;
import com.see51.demo.see51demo.DevStreamActivity;
import com.see51.demo.see51demo.DivDirActivity;
import com.see51.demo.see51demo.R;
import com.see51.demo.see51demo.See51App;
import com.see51.sdk.java.Jni51SeeSdk;

import java.util.ArrayList;
import java.util.List;


public class DevListFragment extends Fragment
{

    DevListFragment.ListDevAdapter m_DevListAp;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_dev_list, container, false);

        ListView lst_dir = (ListView) view.findViewById(R.id.devlist);
        m_DevListAp = new ListDevAdapter();
        m_DevListAp.m_DevBtnVideoListener= mDevBtnVideoListener;
        m_DevListAp.m_DevBtnDirListener  = mDevBtnDirListener;

        final See51App app = (See51App) this.getActivity().getApplication();
        for(int i =0;i<app.m_DevList.size();i++)
        {
            DevInfo dev=    app.m_DevList.get(i);
            m_DevListAp.Add(dev);
        }
        lst_dir.setAdapter( m_DevListAp);
        lst_dir.setOnItemClickListener( m_DevListAp);
        return view;
    }


    public static abstract class DevBtnVideoListener implements View.OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            OnBtnClick((Integer) v.getTag(), v);
        }
        public abstract void OnBtnClick(int position, View v);
    }

    DevBtnVideoListener mDevBtnVideoListener = new DevBtnVideoListener()
    {
        @Override
        public void OnBtnClick(int position, View v)
        {
            DevInfo Item =  m_DevListAp.DataList.get(position);
            ///设备在线启动播放器
            if(Item.OnLine==2) {
                Jni51SeeSdk sdk = new Jni51SeeSdk();
                Intent intent = new Intent();
                String xx = "51See:" + Item.DevId + ":" + sdk.JniParseMediaIPFromUrl(Item.Ssp);
                intent.putExtra("MoviePath", xx);
                intent.putExtra("RaType", Integer.toString(0));
                intent.putExtra("kkMediacodec", Integer.toString(0));

                intent.putExtra("DevId", Item.DevId);
                intent.putExtra("DevTurnIp",sdk .JniParseMediaIPFromUrl(Item.Ssp));

                //指定intent要启动的类
                intent.setClass(v.getContext(), CPlayerActivity.class);//(context.this, Activity02.class);
                v.getContext().startActivity(intent);
            }
        }
    };

    public static abstract class DevBtnDirListener implements View.OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            OnBtnClick((Integer) v.getTag(), v);
        }
        public abstract void OnBtnClick(int position, View v);
    }
    DevBtnDirListener mDevBtnDirListener = new DevBtnDirListener()
    {
        @Override
        public void OnBtnClick(int position, View v)
        {
            DevInfo Item =  m_DevListAp.DataList.get(position);
            ///设备在线启动播放器
            if(Item.OnLine==2) {

               // DevListFragment.this.getActivity();
                Jni51SeeSdk sdk = new Jni51SeeSdk();
                Intent intent = new Intent(v.getContext(), DivDirActivity.class);

                intent.putExtra("DevId", Item.DevId);

                intent.putExtra("DevTurnIp",sdk .JniParseMediaIPFromUrl(Item.Ssp));

                startActivity(intent);
            }
        }
    };
    DevBtnDirListener mDevStreamListener = new DevBtnDirListener()
    {
        @Override
        public void OnBtnClick(int position, View v)
        {
            DevInfo Item =  m_DevListAp.DataList.get(position);
            ///设备在线启动播放器
            if(Item.OnLine==2) {

                // DevListFragment.this.getActivity();
                Jni51SeeSdk sdk = new Jni51SeeSdk();
                Intent intent = new Intent(v.getContext(), DevStreamActivity.class);

                intent.putExtra("DevId", Item.DevId);

                intent.putExtra("DevTurnIp",sdk .JniParseMediaIPFromUrl(Item.Ssp));

                startActivity(intent);
            }
        }
    };
    //设备列表适配器
    class ListDevAdapter extends BaseAdapter implements AdapterView.OnItemClickListener
    {

        private List<DevInfo> DataList = new ArrayList<DevInfo>();//实体类
        public DevBtnVideoListener m_DevBtnVideoListener;
        public DevBtnDirListener m_DevBtnDirListener;
        public void Add(DevInfo item)
        {
            DataList.add(item);
        }
        public void Clear()
        {
            DataList.clear();
        }
        public int getCount()
        {
            // TODO Auto-generated method stub
            return DataList.size();//数目
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {

            LayoutInflater inflater = DevListFragment.this.getActivity().getLayoutInflater();
            View view;

            if (convertView==null) {
                //因为getView()返回的对象，adapter会自动赋给ListView
                view = inflater.inflate(R.layout.devitem, null);
            }else{
                view=convertView;
                //Log.i("info","有缓存，不需要重新生成"+position);
            }

            TextView tv1 = (TextView) view.findViewById(R.id.Txt_DevName);//找到Textviewname
            tv1.setText("设备:"+DataList.get(position).DevName+":"+DataList.get(position).DevId);//设置参数

            Button Btn_Play = (Button) view.findViewById(R.id.Btn_Play);
            Button Btn_DevDir = (Button) view.findViewById(R.id.Btn_DevDir);
            Button Btn_Stream = (Button) view.findViewById(R.id.Btn_Stream);
            TextView Txt_DevOnline = (TextView) view.findViewById(R.id.Txt_DevOnline);//找到Textviewname
            if(DataList.get(position).OnLine==2) {
                Btn_Play.setEnabled(true);
                Btn_DevDir.setEnabled(true);
                Txt_DevOnline.setText("在线");//设置参数
                Btn_DevDir.setTag(position);
                Btn_Play.setTag(position);
                Btn_Play.setOnClickListener( mDevBtnVideoListener);
                Btn_DevDir.setOnClickListener( mDevBtnDirListener);
                Btn_Stream.setEnabled(true);
                Btn_Stream.setTag(position);
                Btn_Stream.setOnClickListener(mDevStreamListener);
            }else{
                Btn_Play.setEnabled(false);
                Btn_DevDir.setEnabled(false);
                Btn_Stream.setEnabled(false);
                Txt_DevOnline.setText("离线");//设置参数
            }

            return view;
        }
        @Override
        public long getItemId(int position) {//取在列表中与指定索引对应的行id
            return 0;
        }
        @Override
        public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
            return null;
        }

        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            Log.d("点击：","点击");
            DevInfo Item = DataList.get(position);
            ///设备在线启动播放器
            if(Item.OnLine==2) {
                Jni51SeeSdk sdk = new Jni51SeeSdk();
                Intent intent = new Intent();
                String xx = "51See:" + Item.DevId + ":" + sdk.JniParseMediaIPFromUrl(Item.Ssp);
                intent.putExtra("MoviePath", xx);
                intent.putExtra("RaType", Integer.toString(0));
                intent.putExtra("kkMediacodec", Integer.toString(0));
                //指定intent要启动的类
                intent.setClass(view.getContext(), CPlayerActivity.class);//(context.this, Activity02.class);
                view.getContext().startActivity(intent);
            }
        }
    }
}
